package vip.liux.contracts.infrastructure.websocket.interceptor;

import org.jetbrains.annotations.NotNull;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import vip.liux.contracts.infrastructure.websocket.WebSocketFrameworkUtils;
import vip.liux.core.current.ICurrentUser;

import java.util.Map;

public class JwtHandshakeInterceptor implements HandshakeInterceptor {

    @Override
    public boolean beforeHandshake(
            @NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response,
            @NotNull WebSocketHandler wsHandler, @NotNull Map<String, Object> attributes) {
        // todo 这里可以添加对请求头的验证逻辑，例如验证 JWT 令牌等
        ICurrentUser<Long> currentUser = null;
        if (currentUser != null) {
            WebSocketFrameworkUtils.setCurrentUser(currentUser, attributes);
        }
        return true;
    }

    @Override
    public void afterHandshake(
            @NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response,
            @NotNull WebSocketHandler wsHandler, Exception exception) {
    }

}